package com.wx.app.mylib.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author huangsm
 */
public class Md5Helper {

    /**
     * md5 加密
     *
     * @param key
     * @return
     */
    public static String md5Encode(String key) {
        byte[] hash = null;
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(key.getBytes());
            hash = md.digest();
        } catch (NoSuchAlgorithmException e) {

        }
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            if ((0xff & hash[i]) < 0x10) {
                hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
            } else {
                hexString.append(Integer.toHexString(0xFF & hash[i]));
            }
        }
        return hexString.toString();
    }
}
